from lib.tools import cxt_connetMyql
from conf.setting import MYSQL_INFO

'''
一、基础费用明细
收费方式：等本等息
月息费率：2%
每月利息 = 合同金额*月息费率
每月还款总额 = 合同金额/期数 + 合同金额*月息费率
'''
userId = 186025
con = cxt_connetMyql(**MYSQL_INFO)
res = con.executeSelectSql('SELECT * FROM tt_order WHERE user_id = "%d";'%userId)
print(type(res[0]['order_money']))

period = 6
contractAmount = int(res[0]['order_money'])
monthlyInterest = contractAmount * 0.02
monthlyPrincipal = contractAmount/period
totalMonthlyRepayment = contractAmount/period + contractAmount*0.02
print('每月本金:%s,每月利息:%s,每月还款总额:%s'%(monthlyPrincipal,monthlyInterest,totalMonthlyRepayment))

'''
1、罚息收费规则：
当期逾期罚息 = 当期还款额*5% + 应还未还本金*0.1%*逾期天数
逾期天数：应还款日期T+1日开始计算罚息。
2、多期逾期时，单期还款需要从最早未还期开始还，可以提前结清

'''
currentPeriod = 1
overdueDays = 125
overdueMoney = totalMonthlyRepayment*0.05 + (contractAmount-(currentPeriod-1)*monthlyPrincipal)*0.001*overdueDays
print('当期逾期罚息金额为:%s'%overdueMoney)



'''
三、提前结清及收费规则
1、提前结清违约金规则：
提前结清违约金 = 剩余期未还本金*N
其中，放款日15天（包括15天）内，N=2%
放款日15天后，N=3%

2、提前结清时还款金额计算
用户提前结清时的还款总额 = 当期本息+历史应还本息+累计应收罚息+剩余期未还本金+剩余未还本金*N
注：应还款日和应还款日之前提前结清，利息收到当期，应还款日之后提前结清，利息收到下一期。

'''
print(res[0]['pay_time'])
top_res = con.executeSelectSql('SELECT * FROM tt_order_plan WHERE user_id = "%d";'%userId)
print(top_res[0]['repay_time'])
# repayTime = 1558336617
# paytime = 1556695017
# dayNum = (repayTime-paytime)/(24*60*60)
# if dayNum <= 15:
#     settledPenalty= (contractAmount-(duePeriod-1)*monthlyPrincipal)*0.02
#     settled_repayMoney=
# elif dayNum > 15:
#     settledPenalty = (contractAmount-(duePeriod-1)*monthlyPrincipal)*0.03
#
# print(dayNum,settledPenalty)
